package servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;

/**
 * Created by xiaoyu on 2016/12/12.
 */
public class QueryTicketServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer startStation;
        Integer endStation;
        String start = request.getParameter("start");
        String end = request.getParameter("end");
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123");

            String sql = "select s." + start + ",e." + end + " from start s left join end e on 1=1";
            System.out.println(sql);
            PreparedStatement pstmt = conn.prepareStatement(sql);
            ResultSet resultSet = pstmt.executeQuery(sql);
            while (resultSet.next()) {
                startStation = resultSet.getInt(1);
                endStation = resultSet.getInt(2);
                System.out.println("count1:" + startStation + "-------" + "count2:" + endStation);
                Integer remain = startStation > endStation ? endStation : startStation;
                response.setHeader("Content-type", "text/html;charset=UTF-8");
                response.setCharacterEncoding("UTF-8");
                response.getWriter().write("从" + start + "到" + end + "还有" + remain + "张票");
                response.getWriter().write("<a href='/order.jsp'>订票</a>");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
